/*********************************************************************
 * 
 * CHINA TELECOM CORPORATION CONFIDENTIAL
 * ______________________________________________________________
 * 
 *  [2015] - [2020] China Telecom Corporation Limited, 
 *  All Rights Reserved.
 * 
 * NOTICE:  All information contained herein is, and remains
 * the property of China Telecom Corporation and its suppliers,
 * if any. The intellectual and technical concepts contained 
 * herein are proprietary to China Telecom Corporation and its 
 * suppliers and may be covered by China and Foreign Patents,
 * patents in process, and are protected by trade secret  or 
 * copyright law. Dissemination of this information or 
 * reproduction of this material is strictly forbidden unless prior 
 * written permission is obtained from China Telecom Corporation.
 **********************************************************************/
package com.shujia.util;


import java.awt.geom.Point2D;
import java.math.BigDecimal;

public class Grid {

	/**
	 * 获取网格中心点
	 * @param gridId 网格id
	 * @return 中心点坐标
	 */
	public static Point2D.Double getCenter(Long gridId){
		double[] bound = Grid.getBound(gridId);
		double x = (bound[0]+bound[1])/2;
		double y = (bound[2]+bound[3])/2;
		return new Point2D.Double(x, y);
	}

	/**
	 * 根据ID获取网格覆盖范围, 使用了BigDecimal确保精度，但速度稍慢。查询经纬度对应网格编号请使用getGridID系列方法
	 *
	 * @param gridID
	 *            网格ID
	 * @return [xmin, xmax, ymin, ymax]，精确到小数点后第四位
	 */
	public static double[] getBound(long gridID) {
		double[] bound = new double[4];
		int z = (int) (gridID % 100) / 10;
		com.shujia.util.GridLevel zLevel = com.shujia.util.GridLevel.levelOfZ(z);
		//double length = zLevel.length();
		BigDecimal length = BigDecimal.valueOf(zLevel.length());
		gridID /= 100;
		// ymin = y
		bound[2] = (gridID % 1000000) / 10000.00;
		// ymax = y+length\
		//BIgDecimal可替换为double, 但精度有问题
		//bound[3] = bound[2]+length;
		bound[3] = length.add(BigDecimal.valueOf(bound[2])).doubleValue();
		gridID /= 1000000;
		// xmin = x
		bound[0] = gridID / 10000.00;
		// xman = x+length
		//bound[1] = bound[0]+length;
		bound[1] = length.add(BigDecimal.valueOf(bound[0])).doubleValue();
		return bound;
	}
}
